/**
 * 2013 2013-5-9
 * @author sfkdjghkjdfhg
 */
package com.surfilter.dp.view;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import com.surfilter.dp.context.ContextHolder;
import com.surfilter.dp.model.ActionModel;
import com.surfilter.dp.model.ModuleModel;
import com.surfilter.dp.util.ViewUtil;

/**
 * @author sfkdjghkjdfhg
 *
 */
public class ActionAddDialog extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField textField_actionName;
	private JTextField textField_actionText;
	private JButton addBtn;
	private JButton cancelBtn;
	/**
	 * Launch the application.
	 */
	/**
	 * Create the dialog.
	 */
	public ActionAddDialog(JFrame frame) {
		super(frame);
		initView();
		initListeners();
	}
	/**
	 * 
	 */
	private void initListeners() {
		final ContextHolder context = ContextHolder.getInstance();
		final ActionAddDialog dialog = this;
		addBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				ModuleModel model = context.getModuleTree().getSelectedModule();
				ActionModel action = new ActionModel();
				action.setActionName(textField_actionName.getText());
				action.setActionText(textField_actionText.getText());
				action.setModuleId(model.getId());
				context.getActionDao().addNewAction(action);
				List<ActionModel> actions = context.getActionDao().getActionByModuleId(model.getId());
				context.getActionPanel().setTableData(actions);
				dialog.dispose();
				JOptionPane.showMessageDialog(context.getMainWin(), "添加action成功");
			}
		});
		cancelBtn.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				dialog.dispose();
			}
		});
	}
	/**
	 * 
	 */
	private void initView() {
		setModal(true);
		setTitle("输入aciton信息");
		setBounds(100, 100, 384, 233);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		{
			JLabel lblNewLabel = new JLabel("action名称：");
			lblNewLabel.setBounds(40, 32, 94, 28);
			contentPanel.add(lblNewLabel);
		}
		
		textField_actionName = new JTextField();
		textField_actionName.setBounds(160, 35, 190, 24);
		contentPanel.add(textField_actionName);
		textField_actionName.setColumns(10);
		
		JLabel lblAction = new JLabel("action描述：");
		lblAction.setBounds(40, 83, 94, 28);
		contentPanel.add(lblAction);
		
		textField_actionText = new JTextField();
		textField_actionText.setColumns(10);
		textField_actionText.setBounds(160, 86, 190, 24);
		contentPanel.add(textField_actionText);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				addBtn = new JButton("添加");
				addBtn.setActionCommand("OK");
				buttonPane.add(addBtn);
				getRootPane().setDefaultButton(addBtn);
			}
			{
				cancelBtn = new JButton("取消");
				cancelBtn.setActionCommand("Cancel");
				buttonPane.add(cancelBtn);
			}
		}
		ViewUtil.toCenter(this);
	}
}
